package com.surekam.hometown.util.coder;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;

import cn.wiseda.finance.utils.encode.ByteUtil;

public class HMacMD5JM {
	public static void main(String[] args) throws Exception {
				
		for (int i = 0; i < 10; i++) {
			String hmacMd5 = HMacMD5JM.hmacMd5("wweicsdn");
			System.out.println(hmacMd5);
		}
	}

	public static String hmacMd5(String key) throws NoSuchAlgorithmException, InvalidKeyException {
		// Generate secret key for HMAC-MD5
		KeyGenerator kg = KeyGenerator.getInstance("HmacMD5");
		SecretKey sk = kg.generateKey();

		// Get instance of Mac object implementing HMAC-MD5, and
		// initialize it with the above secret key
		Mac mac = Mac.getInstance("HmacMD5");
		mac.init(sk);
		byte[] result = mac.doFinal(key.getBytes());
		String hexString = ByteUtil.toHexString(result);
		return hexString;

	}

}
